package com.archive.forum.controller;

import com.archive.forum.dto.CommentCreateDTO;
import com.archive.forum.dto.CommentDTO;
import com.archive.forum.dto.CommentUpdateDTO;
import com.archive.forum.dto.ResultDTO;
import com.archive.forum.enums.CommentTypeEnum;
import com.archive.forum.exception.ExceptionEnum;
import com.archive.forum.mapper.CommentMapper;
import com.archive.forum.model.Comment;
import com.archive.forum.model.CommentWithBLOBs;
import com.archive.forum.model.User;
import com.archive.forum.service.CommentService;
import com.archive.forum.service.PostService;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private CommentService commentService;
    @Autowired
    private PostService postService;
    //发布评论
    @ResponseBody
    @RequestMapping(value = "/comment",method = {RequestMethod.POST})
    public Object post(@RequestBody CommentCreateDTO commentCreateDTO,
                       HttpServletRequest request, Model model){
        User user =  (User) request.getSession().getAttribute("user");
        if (user == null){
            return ResultDTO.errorOf(ExceptionEnum.NOT_LOGIN);
        }
        if (commentCreateDTO == null || StringUtils.isBlank(commentCreateDTO.getContent())|| StringUtils.isBlank(commentCreateDTO.getPlainText())){
            return ResultDTO.errorOf(ExceptionEnum.CONTENT_IS_EMPTY);
        }
        CommentWithBLOBs comment = new CommentWithBLOBs();
        comment.setParentId(commentCreateDTO.getParentId());
        comment.setContent(commentCreateDTO.getContent());
        comment.setType(commentCreateDTO.getType());
        comment.setPlainText(commentCreateDTO.getPlainText());
        comment.setGmtCreate(System.currentTimeMillis());
        comment.setGmtModified(System.currentTimeMillis());
        comment.setCommentator(user.getId());
        comment.setLikeCount(0);
        comment.setCommentCount(0);
        commentService.insert(comment,user);
        return ResultDTO.okOf();
    }
    //获取二级评论
    @ResponseBody
    @RequestMapping(value = "/comment/{id}",method = {RequestMethod.GET})
    public ResultDTO<List<CommentDTO>> comments(@PathVariable(name = "id")Integer id, HttpServletRequest request){
        // 获取当前用户ID，未登录时为null
        Integer userId = null;
        User user = (User) request.getSession().getAttribute("user");
        if (user != null) {
            userId = user.getId();
        }
        List<CommentDTO> comment = commentService.listByTargetId(id, CommentTypeEnum.COMMENT, userId);
        return ResultDTO.okOf(comment);
    }

    @ResponseBody
    @RequestMapping(value = "/comment/{id}", method = RequestMethod.DELETE)
    public ResultDTO deleteComment(@PathVariable(name = "id") Integer id,
                                   HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return ResultDTO.errorOf(ExceptionEnum.NOT_LOGIN);
        }

        Comment comment = commentService.getById(id);
        if (comment == null) {
            return ResultDTO.errorOf(ExceptionEnum.COMMENT_NOT_FOUND);
        }

        // 验证当前用户是否是评论所有者
        if (!user.getId().equals(comment.getCommentator())) {
            return ResultDTO.errorOf(ExceptionEnum.DELETE_FAIL);
        }
        commentService.deleteComment(id);
        return ResultDTO.okOf();
    }
    @ResponseBody
    @PostMapping("/comment/update")
    public ResultDTO updateComment(@RequestBody CommentUpdateDTO commentUpdateDTO,
                                   HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return ResultDTO.errorOf(ExceptionEnum.NOT_LOGIN);
        }

        CommentWithBLOBs comment = commentService.getById(commentUpdateDTO.getId());
        if (comment == null) {
            return ResultDTO.errorOf(ExceptionEnum.COMMENT_NOT_FOUND);
        }

        if (!comment.getCommentator().equals(user.getId())) {
            return ResultDTO.errorOf(ExceptionEnum.EDIT_ERROR);
        }

        comment.setContent(commentUpdateDTO.getContent());
        comment.setPlainText(commentUpdateDTO.getPlainText());
        comment.setGmtModified(System.currentTimeMillis());

        commentService.update(comment);
        return ResultDTO.okOf();
    }
}
